package com.jcl.recruit.verifty.mapper;

import com.jcl.recruit.verfity.model.VerificationCode;
import org.apache.ibatis.annotations.*;

import java.util.Date;

/**
 * @ClassName VerificationCodeMapper

 * @Author yyliu
 * @Date 2018/9/18 9:27
 * @Version 1.0
 **/
public interface VerificationCodeMapper {

    @Select({
            "<script>",
            "select * from verification_code",
            "<where>",
            "<if test=\"null != receiver\"> receiver = #{receiver} </if>",
            "<if test=\"null != type\"> and type = #{type} </if>",
            "</where>",
            "</script>"
    })
    VerificationCode findOneByType(@Param("receiver") String receiver, @Param("type") Integer type);

    @Select({
            "<script>",
            "select * from verification_code",
            "<where>",
            "<if test=\"null != receiver\"> receiver = #{receiver} </if>",
            "<if test=\"null != type\"> and type = #{type} </if>",
            "<if test=\"null != abateDate\"> and abate_date > #{abateDate} </if>",
            "</where>",
            "</script>"
    })
    VerificationCode validateTime(@Param("receiver") String receiver,
                                  @Param("type") Integer type,
                                  @Param("abateDate") Date abateDate);

    @Select({
            "<script>",
            "select * from verification_code",
            "<where>",
            "<if test=\"null != receiver\"> receiver = #{receiver} </if>",
            "<if test=\"null != type\"> and type = #{type} </if>",
            "<if test=\"null != code\"> and code = #{code} </if>",
            "</where>",
            "</script>"
    })
    VerificationCode validateCode(@Param("receiver") String receiver,
                                  @Param("type") Integer type,
                                  @Param("code") String code);


    @Insert({
            "insert into verification_code (receiver, type, code, abate_date) ",
            "values ( ",
            "#{receiver, jdbcType=VARCHAR}, #{type, jdbcType=INTEGER}, #{code, jdbcType=VARCHAR}, #{abateDate, jdbcType=TIMESTAMP}",
            ")"
    })
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int add(VerificationCode verificationCode);

    @Delete({
            "<script>",
            "delete from verification_code",
            "<where>",
            "<if test=\"null != receiver\"> receiver = #{receiver} </if>",
            "<if test=\"null != type\"> and type = #{type} </if>",
            "</where>",
            "</script>"
    })
    int delete(@Param("receiver") String receiver, @Param("type") Integer type);

}
